package com.wanmait.myhouse.dao;
import com.wanmait.myhouse.util.DBHelper;
import com.wanmait.myhouse.vo.Room;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import java.sql.SQLException;
import java.util.List;
public class RoomDAO {


    public void deleteById(Room room){
        QueryRunner queryRunner = new QueryRunner(DBHelper.getDruidDataSource());
        String sql = "delete from t_room where id=?";
        try {
            queryRunner.update(sql,room.getId());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }


    public void MgAddRoom(Room room){
        QueryRunner queryRunner = new QueryRunner(DBHelper.getDruidDataSource());
        String sql = "insert into t_room(room_num) values(?)";
        try {
            queryRunner.update(sql,room.getRoomNum());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    public Room findByNum(Room room){
        QueryRunner queryRunner = new QueryRunner(DBHelper.getDruidDataSource());
        String sql = "select * from t_room where room_num=?";
        try {
            Room room1 = queryRunner.query(sql, new BeanHandler<>(Room.class), room.getRoomNum());
            return room1;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }


    public List<Room> FindAll(){//查找所有house
        QueryRunner queryRunner = new QueryRunner(DBHelper.getDruidDataSource());
        String sql = "select *,room_num roomNum from t_room;";
        try {
            List<Room>typeList = queryRunner.query(sql,new BeanListHandler<>(Room.class));
            return typeList;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }
}
